class Solution {
    public int minOperations(int[] nums, int x) {
        int sum=0;
        for(int a:nums){
            sum+=a;
        }
        int target=sum-x;
        int ret=-1;
        if(target<0) return -1;
        int left=0,right=0,tmp=0;
        int n=nums.length;
        while(right<n){
            tmp+=nums[right];
            while(tmp>target){
                tmp-=nums[left++];
            } 
            if(target==tmp)
                ret=Math.max(ret,right-left+1);
            right++;
        }
        return ret==-1?-1:n-ret;
    }
}